package com.jiatuxueyuan.the4.remoteio;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AutoCloseChannelHandler extends ChannelDuplexHandler {

    private static Logger logger = LoggerFactory.getLogger(AutoCloseChannelHandler.class);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
          if (evt instanceof IdleStateEvent) {
                IdleStateEvent e = (IdleStateEvent)evt;
              if (e.state() == IdleState.READER_IDLE) {
                  logger.warn("60秒没收到客户端的消息，可能对方已经宕机了，关闭当前连接...");
                  ctx.close();
              } /*else if (e.state() == IdleState.WRITER_IDLE) {
                  logger.warn("30秒没有发消息给客户端了，给它发一个保持心跳的消息吧");
                  ctx.writeAndFlush("fuck you!");
              }*/
          }
    }
}